package com.toommi.refuse.controller;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.RecyclablePrice;
import com.toommi.refuse.exception.AdminException;
import com.toommi.refuse.exception.RecycleRecordException;
import com.toommi.refuse.service.RecyclablePriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * ..
 * 可回收物价格管理操作
 *
 * @author yangg
 * @since 2019-07-31 12:27
 */
@RestController
@RequestMapping(value = "")
@Slf4j
@AllArgsConstructor
@PermissionController(groupCode = "admin_recyclable_price", groupName = "可回收物价格管理")
@Api(tags = "可回收物价格管理")
public class RecyclablePriceController {

    private RecyclablePriceService recyclablePriceService;

    /**
     * 新增可回收物回收价格
     *
     * @param recyclablePrice 可回收物回收价格
     * @return 操作结果
     */
    @ApiOperation(value = "新增可回收物价格", notes = "新增可回收物价格")
    @ApiImplicitParam(name = "recyclablePrice", value = "可回收物价格信息", required = true, dataType = "recyclablePrice")
    @RequiresPermissions("admin:recyclable:price:add")
    @PermissionReport(value = "admin:recyclable:price:add", name = "新增价格", group = "admin_recyclable_price")
    @PostMapping(value = "/admin/recyclable/price", produces = "application/json;charset=UTF-8")
    public CommonResponse addRecyclablePrice(@RequestBody RecyclablePrice recyclablePrice) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            recyclablePriceService.addRecyclablePrice(recyclablePrice);
        } catch (AdminException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add recyclable price error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 更新可回收物回收价格
     *
     * @param recyclablePrice 回收价格
     * @return 更新结果
     */
    @ApiOperation(value = "更新可回收物价格", notes = "更新可回收物价格")
    @ApiImplicitParam(name = "recyclablePrice", value = "可回收物价格信息", required = true, dataType = "recyclablePrice")
    @RequiresPermissions("admin:recyclable:price:update")
    @PermissionReport(value = "admin:recyclable:price:update", name = "编辑价格", group = "admin_recyclable_price")
    @PutMapping(value = "/admin/recyclable/price", produces = "application/json;charset=UTF-8")
    public CommonResponse updateRecyclablePrice(@RequestBody RecyclablePrice recyclablePrice) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            recyclablePriceService.updateRecyclablePrice(recyclablePrice);
        } catch (AdminException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("update recyclable price error:{}", e);
            commonResponse = CommonResponse.serverError();
        }

        return commonResponse;
    }


    /**
     * 获取可回收物回收价格
     *
     * @param refuseTypeId  可回收物分类类型
     * @param neighbourhood 小区id
     * @param date          日期
     * @return 指定日期指定小区指定可回收物类型价格
     */
    @ApiOperation(value = "获取可回收物价格", notes = "获取可回收物价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "refuseTypeId", value = "可回收物类型", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "neighbourhood", value = "小区id", required = true, dataType = "Long")
    })
    @GetMapping(value = "/api/recyclable/price", produces = "application/json;charset=UTF-8")
    public CommonResponse findRecyclablePrices(
            @RequestParam(required = false, defaultValue = "-1") long refuseTypeId,
            @RequestParam(required = false) long neighbourhood,
            @RequestParam String date) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(recyclablePriceService.findRecyclablePrice(refuseTypeId, neighbourhood, date));
        } catch (Exception e) {
            log.error("find recyclable price error:{}", e);
            commonResponse = CommonResponse.serverError();
        }

        return commonResponse;
    }

    /**
     * APP端扫码称重后计算总价
     *
     * @param qrcode          二维码
     * @param subRefuseTypeId 子类id
     * @param weight          重量
     * @return 总价计算结果
     */
    @ApiOperation(value = "计算可回收物金额", notes = "计算可回收物金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrcode", value = "识别出的二维码内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "subRefuseTypeId", value = "可回收物类型", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "weight", value = "重量", required = true, dataType = "double")
    })
    @GetMapping(value = "/api/recyclable/calculation", produces = "application/json;charset=UTF-8")
    public CommonResponse calculateMoney(String qrcode, int subRefuseTypeId, double weight) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(recyclablePriceService.calculateMoney(qrcode, subRefuseTypeId, weight));
        } catch (RecycleRecordException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("calculate recyclable money error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


}
